package DARCOShared.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class NetUtils {
	public static void setServerIP(){
		try{
			URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
			URLConnection connection = whatismyip.openConnection();
			connection.addRequestProperty("Protocol", "Http/1.1");
			connection.addRequestProperty("Connection", "keep-alive");
			connection.addRequestProperty("Keep-Alive", "1000");
			connection.addRequestProperty("User-Agent", "Web-Agent");

			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			String ip = in.readLine(); //you get the IP as a String
			String username = URLEncoder.encode("master_t", "UTF-8");
			String password = URLEncoder.encode(MachineSpecificConstants.noIpPass, "UTF-8");
			String hostname = "darco.no-ip.org";
			String hostnameLocal = "darco-local.no-ip.org";
			
			//real ip
			{
			URL url = new URL( "http://dynupdate.no-ip.com/nic/update?hostname="+hostname+"&myip="+ip);           
			URLConnection NOIPconnection = url.openConnection();
			String credentials = username + ":" + password;
			String encoding = Base64.encodeBytes(credentials.getBytes("UTF-8"));
			NOIPconnection.setRequestProperty("Authorization", String.format("Basic %s", encoding));
			BufferedReader NOIPin = new BufferedReader(new InputStreamReader(NOIPconnection.getInputStream()));
			String result = NOIPin.readLine(); //you get the IP as a String
			NOIPin.close();
			}
			
			{
			URL url = new URL( "http://dynupdate.no-ip.com/nic/update?hostname="+hostnameLocal+"&myip="+MachineSpecificConstants.getLocalIP()+"/");
			URLConnection NOIPconnection = url.openConnection();
			String credentials = username + ":" + password;
			String encoding = Base64.encodeBytes(credentials.getBytes("UTF-8"));
			NOIPconnection.setRequestProperty("Authorization", String.format("Basic %s", encoding));
			BufferedReader NOIPin = new BufferedReader(new InputStreamReader(NOIPconnection.getInputStream()));
			String result = NOIPin.readLine(); //you get the IP as a String
			NOIPin.close();
			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}


	public static String getServerIP(){
		//return "151.42.54.97";
		try
		{
			java.net.InetAddress inetAdd = java.net.InetAddress.getByName("darco.no-ip.org");
			String addr = inetAdd.getHostAddress();
			return addr;
		}
		catch(java.net.UnknownHostException uhe)
		{
			uhe.printStackTrace();
			return null;
		}
	}
	
	public static String getLocalServerIP(){
		//return "151.42.54.97";
		try
		{
			java.net.InetAddress inetAdd = java.net.InetAddress.getByName("darco-local.no-ip.org");
			String addr = inetAdd.getHostAddress();
			return addr;
		}
		catch(java.net.UnknownHostException uhe)
		{
			uhe.printStackTrace();
			return null;
		}
	}
}
